﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using JVP.Gateway;
using System.Collections;

namespace JeevanVaisya
{
    public partial class DownLine : Form
    {
        #region Variables
        Gateway gateway = new Gateway();
        int ini;
        SqlDataAdapter da;
        DataSet ds;
        #endregion

        #region Constructor
        public DownLine()
        {
            InitializeComponent();            
            this.Load += new EventHandler(DownLine_Load);
            lstDownLine.DoubleClick += new EventHandler(lstDownLine_DoubleClick);
            lstDownLine.ColumnWidthChanging += new ColumnWidthChangingEventHandler(lstDownLine_ColumnWidthChanging);
            lblEmpCode.Click += new EventHandler(lblEmpCode_Click);
            lblIntroCode.Click += new EventHandler(lblIntroCode_Click);
            tvDownLine.AfterSelect += new TreeViewEventHandler(tvDownLine_AfterSelect);
        }

        #endregion

        #region Events
        void lblIntroCode_Click(object sender, EventArgs e)
        {
            loadList(lblIntroCode.Text);
            lblEmpCode.Text = lblIntroCode.Text;
        }

        void lblEmpCode_Click(object sender, EventArgs e)
        {
            loadList(lblEmpCode.Text);
        }

        void lstDownLine_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
        }

        void lstDownLine_DoubleClick(object sender, EventArgs e)
        {
            lblIntroCode.Text = lblEmpCode.Text;
            lblEmpCode.Text = lstDownLine.Items[lstDownLine.SelectedItems[0].Index].SubItems[1].Text;

            TreeNodeCollection tnc;
            tnc = tvDownLine.Nodes;
            for(int i=0; i< tnc.Count; i++)
                selectNode(tnc, i);
            for (int i = 0; i < tvDownLine.Nodes.Count; i++)
            {
                if (lblEmpCode.Text == tnc[i].Tag.ToString())
                    tnc[i].BackColor = Color.Pink;
                else if (tnc[i].Nodes.Count > 0)
                {
                    for (int j = 0; j < tnc[i].Nodes.Count; j++) { }
                        //selectNode(tnc[j].Nodes);
                }
            } 
            

            TreeNode[] selected = tvDownLine.Nodes.Find(lblEmpCode.Text, true);
            for (int i = 0; i < selected.Length; i++)
            {
                tvDownLine.SelectedNode = selected[i];
                tvDownLine.Select();
                tvDownLine.SelectedNode.BackColor = Color.Blue;
            }
            loadList(lstDownLine.Items[lstDownLine.SelectedItems[0].Index].SubItems[1].Text);
        }

        private void selectNode(TreeNodeCollection tnc, int i)
        {
            int j = 0;
            TreeNode[] res = tvDownLine.Nodes.Find(lblEmpCode.Text, true);
            //tvDownLine.Nodes[res.First().Tag.ToString()].TreeView.Select();
            if (tnc[i].TreeView.HasChildren)
            {
                tnc = tnc[i].Nodes;
                i++;
                selectNode(tnc, j);
            }
        }

        void tvDownLine_AfterSelect(object sender, TreeViewEventArgs e)
        {
            lblEmpCode.Text = tvDownLine.SelectedNode.Tag.ToString();
            lblEmpName.Text = tvDownLine.SelectedNode.Text;
            if (tvDownLine.SelectedNode.Parent != null)
                lblIntroCode.Text = tvDownLine.SelectedNode.Parent.Tag.ToString();
            else
                lblIntroCode.Text = "";
            loadList(tvDownLine.SelectedNode.Tag.ToString());
        }

        #endregion

        #region Load Event
        void DownLine_Load(object sender, EventArgs e)
        {
            da = gateway.getDownLine();            
            DataTable dt = new DataTable();
            TreeNode rootNode=null;
            da.Fill(dt);
            tvDownLine.Nodes.Clear();
            
            Hashtable idToNode = new Hashtable(dt.Rows.Count);            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = dt.Rows[i]["EmpName"].ToString();// + "(" + dt.Rows[i]["BranchCityCode"].ToString() + dt.Rows[i]["EmpCode"].ToString() + ")";
                newNode.Tag = dt.Rows[i]["EmpCode"];
                idToNode.Add(dt.Rows[i]["EmpCode"], newNode);
                if (dt.Rows[i]["IntroducerCode"] == DBNull.Value)
                    rootNode = newNode;
            }

            for(int i=0; i< dt.Rows.Count; i++)
            {
                TreeNode nodeToAdd = (TreeNode)idToNode[dt.Rows[i]["EmpCode"]];
                // find parent
                if(dt.Rows[i]["IntroducerCode"]!= DBNull.Value)
                {
                    TreeNode parentNode = (TreeNode)idToNode[dt.Rows[i]["IntroducerCode"]];
                    parentNode.Nodes.Add(nodeToAdd);
                }
            }

            tvDownLine.Nodes.Add(rootNode);
            tvDownLine.ExpandAll();
        }
        #endregion

        #region Functions
        private void addValues(DataSet ds,ListView lst)
        {
            try
            {
                lst.Items.Clear();
                FontFamily family = new FontFamily("Arial");
                lst.Font = new Font(family,10.0f,FontStyle.Bold);                
                foreach (DataRow textRow in ds.Tables[0].Rows)
                {
                    ListViewItem li = new ListViewItem(textRow[0].ToString());
                    for (int i = 1; i < ds.Tables[0].Columns.Count; i++)
                    { li.SubItems.Add(textRow[i].ToString()); }
                    lst.Items.Add(li);
                }                
            }
            catch
            {

            }
        }
        
        private bool addColumns(DataSet ds, ListView lst)
        {
            try
            {
                lst.Columns.Clear();
                lst.View = View.Details;
                lst.FullRowSelect = true;
                lst.GridLines = true;
                lst.MultiSelect = false;
                FontFamily family = new FontFamily("Verdana");
                lst.Font = new Font(family, 11.0f, FontStyle.Bold);
                int i = 0;
                foreach (DataColumn dtCol in ds.Tables[0].Columns)
                {
                    ColumnHeader header = new ColumnHeader();
                    header.Text = dtCol.Caption;
                    lst.Columns.Add(header);
                    if(i!=1)
                        lst.Columns[i].Width = 120;
                    else
                        lst.Columns[i].Width = 210;                    
                    lst.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.None);
                    i += 1;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void loadList(string designID)
        {
            da = gateway.getInformations(designID);
            ds = new DataSet();
            da.Fill(ds);
            if (addColumns(ds, lstDownLine))
                addValues(ds, lstDownLine);
            lstDownLine.Columns[1].Width = 0;
            lstDownLine.Columns[2].Width = 210;
            ds.Dispose();
            da.Dispose();
            da = gateway.getCustomers(designID);
            ds = new DataSet();
            da.Fill(ds);
            if (addColumns(ds, lstCustomer))
                addValues(ds, lstCustomer);
            ds.Dispose();
            da.Dispose();
        }
        #endregion
    }
}
